import React from 'react';
import {
  FormPanel,
  FormPanelBtnAlign,
  InputType,
  type FormPanelType,
  FormPanelObject,
  FormPanelBtnDisplay,
} from 'lfun-bg-comp';
import { Modal, message } from 'antd';
import { useLocation } from 'react-router-dom';
import { isArray } from 'lodash';
import { institutionCloseAudit, institutionKeyinfoAudit, institutionOpenAudit } from '@/service/unit/institution';
import { schoolCloseAudit, schoolKeyinfoAudit, schoolOpenAudit } from '@/service/unit/school';
import {
  getPlanningSchooWorkflowInfo,
  planningSchoolCloseAudit,
  planningSchoolKeyinfoAudit,
  planningSchoolOpenAudit,
} from '@/service/unit/planning';

const Groups: React.FC<{
  show: boolean;
  onCancel: () => void;
  onFinish: () => void;
}> = ({ show, onCancel, onFinish }) => {
  const location = useLocation();
  const [showCloseInfo, setShowCloseInfo] = React.useState(false);
  const [closeVal, setCloseVal] = React.useState({});
  // const { id } = useParams();
  // const op_id = Number(id);
  const getOptype = () => {
    const path = location.pathname;
    if (path.includes('/open')) {
      return 'open';
    }
    if (path.includes('/close')) {
      return 'close';
    }
    if (path.includes('/changekeyinfo')) {
      return 'keyinfo';
    }
    return '';
  };
  const getOpFun = () => {
    const type = getOptype();
    if (location.pathname.includes('institution-audit')) {
      switch (type) {
        case 'close':
          return institutionCloseAudit;
        case 'open':
          return institutionOpenAudit;
        case 'keyinfo':
          return institutionKeyinfoAudit;
        default:
          return null;
      }
    }
    if (location.pathname.includes('/school-audit/')) {
      switch (type) {
        case 'close':
          return schoolCloseAudit;
        case 'open':
          return schoolOpenAudit;
        case 'keyinfo':
          return schoolKeyinfoAudit;
        default:
          return null;
      }
    }
    if (location.pathname.includes('/planning-audit/')) {
      switch (type) {
        case 'close':
          return planningSchoolCloseAudit;
        case 'open':
          return planningSchoolOpenAudit;
        case 'keyinfo':
          return planningSchoolKeyinfoAudit;
        default:
          return null;
      }
    }
    return null;
  };
  const init = async () => {
    if (getOptype() === 'close') {
      setShowCloseInfo(true);
      const res = await getPlanningSchooWorkflowInfo(location.state.process_instance_id);
      if (res.success) {
        setCloseVal(res.data);
      }
    }
  };
  //
  React.useEffect(() => {
    init();
  }, []);

  const auditTemplate: FormPanelType[] = [
    {
      title: '审核',
      span: 24,
      labelCol: { span: 6 },
      wrapperCol: { span: 18 },
      inputs: [
        {
          label: '信息审核',
          name: 'audit',
          key: 'audit',
          inputType: InputType.select,
          required: true,
          renderOption: [
            {
              label: '审核通过',
              value: '1',
            },
            {
              label: '审核驳回',
              value: '2',
            },
          ],
        },
        {
          label: '审核说明',
          name: 'approve_reason',
          key: 'approve_reason',
        },
      ],
    },
  ];
  return (
    <Modal title="审核" open={show} footer={null} onCancel={onCancel}>
      <FormPanel
        template={
          showCloseInfo
            ? [
                {
                  title: '关闭说明',
                  span: 24,
                  labelCol: { span: 6 },
                  wrapperCol: { span: 18 },
                  inputs: [
                    {
                      label: '关闭原因',
                      name: 'action_reason',
                      key: 'action_reason',
                      canEdit: false,
                    },
                    {
                      label: '相关材料',
                      name: 'related_license_upload',
                      key: 'related_license_upload',
                      inputType: InputType.minioUpload,
                      disabled: true,
                      formatValue: (value: any) => {
                        //value = value || 'https://minio.f123.pub/demo/8868964926.jpg';
                        if (value) {
                          return (isArray(value) ? value : value.split(','))?.map(item => {
                            return { name: item, url: item };
                          });
                        }
                        return [];
                      },
                    },
                  ],
                },
              ].concat(auditTemplate)
            : auditTemplate
        }
        onFinish={async (form: FormPanelObject) => {
          const res = await getOpFun()(
            location.state.process_instance_id,
            form.getFieldValue('approve_reason'),
            form.getFieldValue('audit') === '1'
          );
          if (res.success) {
            message.success('操作成功');
            onFinish();
          } else {
            message.error(res.message || res.details);
          }
        }}
        key={1}
        btnsAlign={FormPanelBtnAlign.center}
        buttonsDisplay={FormPanelBtnDisplay.block}
        // buttonsDisplay={FormPanelBtnDisplay.none}
        values={closeVal}
      />
    </Modal>
  );
};

export default Groups;
